Conversation
tianheng
left a comment
There was a problem hiding this comment.
Please address those comments
|
|
||
| public class Solution { | ||
|
|
||
| public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) { |
There was a problem hiding this comment.
The whole function is too long. Try to break it down to a few pieces
| return wraplist; | ||
| } | ||
|
|
||
| public void dfs(Map<String, Integer> map, Map<String, Set<String>> neighbors, List<String> sublist, List<List<String>> wraplist, String curWord, String endWord){ |
There was a problem hiding this comment.
rename dfs to something meaningful.
| public class Solution { | ||
|
|
||
| public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) { | ||
| Map<String, Integer> map = new HashMap<>(); |
There was a problem hiding this comment.
the name map here is confusing. Actually this is a map that records the shortest distance from begin word.
| int len = queue.size(); | ||
| for(int i = 0; i < len; i++){ | ||
| String curWord = queue.poll(); | ||
| if(map.get(curWord) == -1 || map.get(curWord) > distance){ |
There was a problem hiding this comment.
This condition can be simplified. Think about if such scenario exists: you visit a node, and the node has been visited and it has a larger distance than current distance. Is that possible?
| map.put(curWord, distance); | ||
| } | ||
| if(curWord.equals(endWord)){ | ||
| found = true; |
There was a problem hiding this comment.
The found flag is only used to break the loop. Why don't you break here?
| } | ||
| String newWord = new String(arr); | ||
| if(wordSet.contains(newWord)){ | ||
| if(map.get(newWord) == -1){ |
There was a problem hiding this comment.
This is a duplicated check.
| map.put(word, -1); | ||
| neighbors.put(word, new HashSet<>()); | ||
| } | ||
| int distance = 0; |
There was a problem hiding this comment.
Suggest to rename it to cur_distance
| Set<String> wordSet = new HashSet<>(); | ||
| for(String word : wordList){ | ||
| wordSet.add(word); | ||
| map.put(word, -1); |
There was a problem hiding this comment.
nit: map.put(word, cur_distance - 1);
| queue.offer(beginWord); | ||
| while(!queue.isEmpty()){ | ||
| int len = queue.size(); | ||
| for(int i = 0; i < len; i++){ |
There was a problem hiding this comment.
You do not need this for loop. This for loop does nothing good but complex your logic.
While() {
// Only process one node each time
// Pop element from queue
// Update distance and find neighbors.
// Push neighbors into queue.
}
| return wraplist; | ||
| } | ||
|
|
||
| public void dfs(Map<String, Integer> map, Map<String, Set<String>> neighbors, List<String> sublist, List<List<String>> wraplist, String curWord, String endWord){ |
There was a problem hiding this comment.
So a very good question here, if you can answer this, then you probably know why your program running slow.
What is the difference between
- DFS from begin to end
- DFS from end to begin
No description provided.